using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UI : MonoBehaviour, ISaveable
{
    [SerializeField] private GameObject[] buttonHolderArray;
    [SerializeField] private Button[] buttonArray;
    public GameObject desctionTextBox;
    public UICraftWindow craftWindow;
    public SkillTipUI skillTipUI;
    [SerializeField] private GameObject dieUI;
    [SerializeField] private AudioSliderController[] audioSliderArray;

    public static event Action PlayerDieEvent;
    public static void CallPlayerDieEvent()
    {
        if (PlayerDieEvent != null)
            PlayerDieEvent();
    }

    private void Start()
    {
        for (int i = 0; i < buttonArray.Length; ++i)
        {
            int buttonHolderIndex = i % 4;
            buttonArray[i].onClick.AddListener(delegate { SwitchTo(buttonHolderArray[buttonHolderIndex]); });
        }
    }
    private void OnEnable()
    {
        PlayerDieEvent += PlayerDieUIShow;
        SaveManager.Instance.LoadOverEvent += HideAllUI;
        SaveManager.Instance.LoadStartEvent += ShowAllUI;

    }
    private void OnDisable()
    {
        PlayerDieEvent -= PlayerDieUIShow;
        SaveManager.Instance.LoadStartEvent -= ShowAllUI;
        SaveManager.Instance.LoadOverEvent -= HideAllUI;
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.C))
            SwitchToKey(buttonHolderArray[0]);
        if (Input.GetKeyDown(KeyCode.V))
            SwitchToKey(buttonHolderArray[1]);
        if (Input.GetKeyDown(KeyCode.B))
            SwitchToKey(buttonHolderArray[2]);
        if (Input.GetKeyDown(KeyCode.N))
            SwitchToKey(buttonHolderArray[3]);
    }
    private void PlayerDieUIShow() => dieUI.SetActive(true);
    public void SwitchTo(GameObject menu)
    {
        for (int i = 0; i < transform.childCount; ++i)
        {
            transform.GetChild(i).gameObject.SetActive(false);
        }
        if (menu != null)
            menu.SetActive(true);
    }
    public void SwitchToKey(GameObject menu)
    {
        if (menu != null && menu.activeSelf)
        {
            menu.SetActive(false);
            GameManager.Instance.PauseGame(false);
            return;
        }
        for (int i = 0; i < transform.childCount; ++i)
        {
            transform.GetChild(i).gameObject.SetActive(false);
        }
        menu.SetActive(true);
        GameManager.Instance.PauseGame(true);
    }
    public void ShowAllUI()
    {
        for (int i = 0; i < buttonHolderArray.Length; ++i)
        {
            buttonHolderArray[i].gameObject.SetActive(true);
        }
    }
    public void HideAllUI()
    {
        for (int i = 0; i < buttonHolderArray.Length; ++i)
        {
            buttonHolderArray[i].gameObject.SetActive(false);
        }
    }

    public void Save(ref GameData data)
    {
        data.volumeSettings.Clear();
        foreach (AudioSliderController audio in audioSliderArray)
        {
            Debug.Log("Save" + audio.slider.value);
            data.volumeSettings.Add(audio.name, audio.slider.value);
        }
    }

    public void Load(GameData data)
    {
        foreach (KeyValuePair<string, float> pair in data.volumeSettings)
        {
            foreach (AudioSliderController audio in audioSliderArray)
            {
                if(pair.Key==audio.parametr)
                    audio.LoadSlider(pair.Value);
            }
        }
    }
}
